package com.itheima.spring.bestpractive.controller.login;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.itheima.spring.bestpractive.common.myResult.R;
import com.itheima.spring.bestpractive.pojo.Employee;
import com.itheima.spring.bestpractive.service.EmpService;
import com.itheima.spring.bestpractive.utils.MinioUtil;
import com.itheima.spring.bestpractive.vo.req.employee.EmpLogin2ReqVo;
import com.itheima.spring.bestpractive.vo.req.employee.EmpReqVo;
import com.itheima.spring.bestpractive.vo.resp.employee.EmpRespVo;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @author: HZY
 * @date: 2025/6/25 14:42
 * @Content:
 */
@RestController
public class loginController {
    @Autowired
    private EmpService empService;
    @Autowired
    private MinioUtil minioUtil;

    @GetMapping("/login/background")
    public R loginBackground() {
        String fileName = "bank.png";
        String url = minioUtil.getFile(fileName);
        return R.success(url);
    }

    @PostMapping("/login")
    public R login(@RequestBody @Valid EmpReqVo empReqVo) throws JsonProcessingException {
        System.out.println(empReqVo.toString());
        EmpRespVo emp = empService.getEmp(empReqVo);
        Map<String,Object> map = new HashMap<>();
        map.put("token",emp.getToken());
        map.put("empRole",emp.getUserRole().getRoleId());
        map.put("empId",emp.getEmployeeId());
        map.put("departmentId",emp.getDepartment().getDepartmentId());
        return R.success(map);
    }

    @PostMapping("/login/getEmp")
    public R getEmp(@RequestBody EmpLogin2ReqVo  data){
        long empId = empService.getEmpByPhoneAndId(data);
        return R.success(empId);
    }

    @PostMapping("/login/updataEmp")
    public R updataEmp(@RequestBody EmpReqVo data){
        Employee emp = new Employee();
        BeanUtils.copyProperties(data,emp);
        empService.updateEmpPassword(emp);
        return R.success();
    }

    @DeleteMapping("/login/logout")
    public R logout(@RequestBody Map<String, Long> data) {
        empService.logout(data.get("employeeId"));
        return R.success("登出成功");
    }
}
